/**
 *
 */
package com.lgCore.factory;

import com.lgCore.config.CuratorConfigCenter;
import com.lgCore.consumer.service.IZookServiceClient;
import com.lgCore.consumer.service.impl.ZookClientImpl;
import com.lgCore.model.BaseConnectionInfo;
import com.lgCore.provide.model.ServiceZkData;
import com.lgCore.provide.service.ICommonServiceServer;
import com.lgCore.provide.service.impl.CommonServiceServerImpl;
import org.apache.curator.framework.CuratorFramework;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @author ligang
 * zk服务工厂
 * 如：
 * 客户端解析所有地址
 * 服务端注册所有服务
 * 所有重要信息和总枢纽在此
 */
public class RpcServiceZkFactory {
    //rpc根目录名称
    private static String zkRootPath = BaseConnectionInfo.getZkRootPath();
    private static ICommonServiceServer commonServiceServer;
    private static IZookServiceClient zookServiceClient;
    /**
     * corator 配置中心
     */
    private static CuratorFramework client;
    /**
     * 服务注册需要注入的serviceZk集合
     */
    private static List<ServiceZkData> serviceZkDataList;

    /**
     * 工作环境  gray 走灰色服务，normal 走正常服务 默认normal
     */
    private static String systemRegionType;

    /**
     * 环境 dev,test,prod 默认dev
     */
    private static String profilesActive;

    /**
     * 加载配置数据
     *
     * @return
     */
    public static ICommonServiceServer loadConfig() {
        return commonServiceServer;
    }

    static {
        /**
         * 注入 提供者和消费者
         */
        registerProtocol(new CommonServiceServerImpl(), new ZookClientImpl());
    }


    public static void openConnection(int sessionTimeoutMs, int connectionTimeoutMs, int baseSleepTimeMs, int maxRetries, String connectionString, String zkRootPath) {
        if (client == null) {
            client = new CuratorConfigCenter(sessionTimeoutMs, connectionTimeoutMs, baseSleepTimeMs, maxRetries, connectionString, zkRootPath).getClient();
        }
    }

    public static void openConnection(String connectionString, String zkRootPath,String systemRegion,String profilesActive) {
        if (client == null) {
            client = new CuratorConfigCenter(connectionString, zkRootPath).getClient();
        }
        systemRegionType = systemRegion;
        RpcServiceZkFactory.profilesActive = profilesActive;
    }

    public static void openConnection(String connectionString) {
        if (client == null) {
            client = new CuratorConfigCenter(connectionString).getClient();
        }
    }
    public static void openConnection(String connectionString,String systemRegion,String profilesActive) {
        if (client == null) {
            client = new CuratorConfigCenter(connectionString).getClient();
        }
        systemRegionType = systemRegion;
        RpcServiceZkFactory.profilesActive = profilesActive;
    }

    private static void registerProtocol(ICommonServiceServer serverHandler, IZookServiceClient clientHandler) {
        commonServiceServer = serverHandler;
        zookServiceClient = clientHandler;
    }

    public static ICommonServiceServer getCommonServiceServer() {
        return commonServiceServer;
    }

    public static IZookServiceClient getZookServiceClient() {
        return zookServiceClient;
    }

    public static void setServiceZkDataList(List<ServiceZkData> serviceZkDataList) {
        RpcServiceZkFactory.serviceZkDataList = serviceZkDataList;
    }

    public static List<ServiceZkData> getServiceZkDataList() {
        return serviceZkDataList;
    }

    public static String getZkRootPath() {
        return zkRootPath;
    }

    public static void setZkRootPath(String zkRootPath) {
        RpcServiceZkFactory.zkRootPath = zkRootPath;
    }

    public static CuratorFramework getClient() {
        return client;
    }

    public static String getSystemRegionType() {
        return systemRegionType;
    }

    public static String getProfilesActive() {
        return profilesActive;
    }
}
